﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DanhSachNguoiDung : System.Web.UI.Page
{
    TiemPhongDataContext db = new TiemPhongDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            loadDataListView();
        }        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            NguoiDung us = new NguoiDung();
            us.FullName = this.txtfullname.Text;
            us.UserName = this.txtusername.Text;
            us.Email = this.txtemail.Text;
            us.Password = MD5.GetMd5Hash(this.txtpassword.Text);
            us.Description = this.txtdescription.Text;
            us.DateCreate = DateTime.ParseExact(this.txtdatecreate.Text, "dd/MM/yyyy", null);
            us.Address = this.txtaddress.Text;
            us.Phone = this.txtphone.Text;
            db.NguoiDungs.InsertOnSubmit(us);
            db.SubmitChanges();
            ClearControl();
            loadDataListView();
            this.LabelMessage.Text = "Thêm người dùng thành công !";
        }
        catch(Exception ex)
        {
            this.LabelMessage.Text = "Thêm User thất bại "+ ex.Message;
        }
    }
    protected void btnUpdate_Click(object sender,EventArgs e) {
    
    }
    protected void ListViewVacxin_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        var idUser = e.CommandArgument.ToString();
        var ResultUser = db.NguoiDungs.FirstOrDefault(v => v.NguoiDungId.Equals(idUser));
        if (e.CommandName == "Delete")
        {
            var deleteResultUserRole = db.UserInRoles.Where(u=>u.NguoiDung.Equals(Convert.ToInt32(ResultUser.NguoiDungId)));
                                       
            if (deleteResultUserRole != null)
            {
                db.UserInRoles.DeleteAllOnSubmit(deleteResultUserRole);
            }
            if (ResultUser != null)
                db.NguoiDungs.DeleteOnSubmit(ResultUser);
            db.SubmitChanges();
            ClearControl();
            loadDataListView();
        }
        if (e.CommandName == "Update")
        {
            txtfullname.Text = ResultUser.FullName;
            txtusername.Text = ResultUser.UserName;
            txtpassword.Text = ResultUser.Password.Trim();
            txtemail.Text = ResultUser.Email.ToString();
            txtdatecreate.Text = Convert.ToDateTime(ResultUser.DateCreate).ToString("d");
            txtaddress.Text = ResultUser.Address;
            txtphone.Text = ResultUser.Phone;
            txtdescription.Text = ResultUser.Description;
            btnUpdate.Enabled = true;
            btnSave.Enabled = false;
            ClearControl();
            loadDataListView();
        }
    }
    protected void ListViewVacxin_OnItemDeleting(object sender, ListViewDeleteEventArgs e) { }
    protected void ListViewVacxin_OnItemUpdating(object sender, ListViewUpdateEventArgs e) { }
    protected void ClearControl()
    {
        txtfullname.Text = String.Empty;
        txtusername.Text = String.Empty;
        txtpassword.Text = String.Empty;
        txtemail.Text = String.Empty;
        txtdescription.Text = String.Empty;
        txtphone.Text = String.Empty;
        txtdatecreate.Text = String.Empty;
        txtaddress.Text = String.Empty;
        txtdatecreate.EnableViewState = true;

    }
    protected void loadDataListView()
    {
        var listUser = db.NguoiDungs.Select(u=> new {u.NguoiDungId,
            u.FullName,u.UserName,u.Email,u.Password,
            DateCreate=u.DateCreate.HasValue?u.DateCreate.ToString():String.Empty,
            DateUpdate=u.DateUpdate.HasValue?u.DateUpdate.ToString():String.Empty,
            u.DateLogin,u.Description,u.Address,u.Phone}).ToList();
        this.ListViewUser.DataSource = listUser;
        this.ListViewUser.DataBind();        
    }
}